package com.woniu.controller;

import com.woniu.aspect.OpreLog;
import com.woniu.constant.Constant;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.dto.CarTypeSearchDTO;
import com.woniu.pojo.CarType;
import com.woniu.service.ICarTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 品牌控制器
 * @author tarnett
 */
@RestController
@RequestMapping("/carType")
@Slf4j
@Api(tags = "类型管理")
public class CarTypeController {

    @Autowired
    private ICarTypeService carTypeService;

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<CarType>> listAll(){
        List<CarType> shopList = carTypeService.listAll();
        return Result.ok(shopList);
    }

    @ApiOperation("搜索分页")
    @PostMapping("/search")
    public Result<PageResult<CarType>> listSearch(@RequestBody CarTypeSearchDTO carTypeSearchDTO){
        // 1. 对数据进行规则校验 TODO
        // 2. 调用业务逻辑层处理数据
        PageResult<CarType> pageResult = carTypeService.listSearch(carTypeSearchDTO);
        // 3. 接收数据，封装响应结果
        // 4. 返回数据
        return Result.ok(pageResult);
    }

    @OpreLog(title = "车辆类型模块", businessType = Constant.BUSINESS_TYPE_SAVE)
    @ApiOperation("保存类型")
    @PostMapping
    public Result save(@RequestBody CarType carType){
        Result result = carTypeService.save(carType);
        return result;
    }

    @OpreLog(title = "车辆类型模块", businessType = Constant.BUSINESS_TYPE_DELETE)
    @ApiOperation("批量删除")
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable Long[] ids){
        Integer row = carTypeService.deleteByIds(ids);
        return Result.commonByRow(row);
    }

    @OpreLog(title = "车辆类型模块", businessType = Constant.BUSINESS_TYPE_UPDATE)
    @ApiOperation("修改类型")
    @PutMapping
    public Result update(@RequestBody CarType carType){
        Result result = carTypeService.updateById(carType);
        return result;
    }

    @ApiOperation("根据ID查询")
    @GetMapping("/{id}")
    public Result<CarType> getById(@PathVariable Long id){
        CarType carType = carTypeService.getById(id);
        return Result.ok(carType);
    }

    @ApiOperation("名称是否存在")
    @GetMapping("/check/name/{name}")
    public Result<CarType> checkNameExist(@PathVariable String name){
        CarType carType = carTypeService.checkNameExist(name);
        return Result.ok(carType);
    }
}
